package cn.hg.solon.youcan.framework.listener;

import org.dromara.hutool.core.text.StrValidator;
import org.dromara.hutool.core.util.ObjUtil;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.event.EventListener;

import com.mybatisflex.core.util.UpdateEntity;

import cn.hg.solon.youcan.common.util.AddressUtil;
import cn.hg.solon.youcan.system.entity.SysUserOnline;
import cn.hg.solon.youcan.system.service.SysUserOnlineService;

/**
 * @author 胡高
 */
@Component
public class UserOnlineEventListener implements EventListener<SysUserOnline> {

    @Inject
    SysUserOnlineService userOnlineService;

    @Override
    public void onEvent(SysUserOnline event) throws Throwable {
        if (ObjUtil.isNull(event) || StrValidator.isBlank(event.getId())) {
            return;
        }

        SysUserOnline online = UpdateEntity.of(SysUserOnline.class);
        online.setId(event.getId());
        online.setLocation(AddressUtil.getRealAddressByIP(event.getIp()));

        this.userOnlineService.updateById(online);
    }

}
